//
//  KIcon.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2020/5/22.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

struct KIconGroup {
    var image:String!
    var title:String?
    init(title:String?,image:String) {
        self.image = image
        self.title = title
    }
}
enum KIconPosition {
    case left
    case right
    case top
    case bottom
}

class KIcon: MSView {
    
    var data:KIconGroup?
   
    var textColor:NSColor = NSColor.white {
        didSet {
            self.title.textColor = textColor
        }
    }
    
    var font:NSFont = NSFont.systemFont(ofSize: 12) {
        didSet {
            self.title.font = font
        }
    }
    
    var imagePosition:KIconPosition = .top {
        didSet{
            self.needPosi(imagePosition: imagePosition)
        }
    }
    
    var backgroundColor:NSColor = color_gray(220).withAlphaComponent(0.1) {
        didSet {
            self.bgView.layer?.backgroundColor = backgroundColor.cgColor
        }
    }
    
    var backgroundHoverColor:NSColor = color_gray(220).withAlphaComponent(0.1) {
        didSet {
            self.bgView.layer?.backgroundColor = backgroundHoverColor.cgColor
        }
    }
    
    
    func needPosi(imagePosition:KIconPosition) {
        let x = (self.title.stringValue.ga_width(font: self.font.pointSize))/2
        var imageY:CGFloat = -5
        var imageX:CGFloat = 0
        
        let tx = CGFloat(22)/2
        var titleX:CGFloat = 0
        switch imagePosition {
        case .left:
            imageY = 0
            imageX = -x
            titleX = tx
        case .right:
            imageY = 0
            imageX = x
            titleX = -tx
        case .top:
            imageY = -5
            imageX = 0
            titleX = 0
        case .bottom:
            imageY = 5
            imageX = 0
            titleX = 0
        }
        self.imageV.snp.updateConstraints { (make) in
            make.centerY.equalToSuperview().offset(imageY)
            make.centerX.equalToSuperview().offset(imageX)
        }
        
        self.title.snp.updateConstraints { (make) in
            make.centerY.equalToSuperview().offset(-imageY)
            make.centerX.equalToSuperview().offset(titleX)
        }
    }
    
    
    //左右空白间距
    var SpaceLR:CGFloat = 16 {
        didSet {
            
        }
    }
    
    var upText:String? {
        didSet {
            guard let tex  = upText else {
                return
            }
            self.title.stringValue = tex
            let wid = tex.ga_width(font: 13)+SpaceLR*2
            self.snp.updateConstraints { (make) in
                make.width.equalTo(wid)
            }
            self.needPosi(imagePosition: self.imagePosition)
        }
    }
    
    var imageString:String = "" {
        didSet {
            let image = NSImage.image(name:imageString) ?? NSImage.init()
            self.imageV.image = NSImage.image(name:imageString)
            self.data?.image = imageString
            self.imageV.snp.updateConstraints { (make) in
                make.size.equalTo(image.size)
            }
        }
    }
    
    //没有背景
    var hasClickEffect:Bool = true {
        didSet {
            self.bgView.wantsLayer = true
            self.bgView.layer?.backgroundColor = NSColor.clear.cgColor
        }
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(data:KIconGroup) {
        self.init()
        self.data = data
        self.setupUI()
        self.loadData()
    }
    
    func loadData() {
        self.status = { state in
            self.selectView.isHidden = true
            switch state {
            case .normal:
                self.bgView.layer?.backgroundColor = self.backgroundColor.cgColor
            case .highlight:
                self.bgView.layer?.backgroundColor = self.backgroundHoverColor.cgColor
            case .hover:
                self.bgView.layer?.backgroundColor = self.backgroundHoverColor.cgColor
            case .selected:
                self.selectView.isHidden = false
                self.bgView.layer?.backgroundColor = self.backgroundColor.cgColor
            }
        }
        
        if let data = self.data {
            self.title.stringValue  = data.title ?? ""
            let image = NSImage.image(name:data.image) ?? NSImage.init()
            self.imageV.image = image
            self.imageV.snp.makeConstraints { (make) in
                make.size.equalTo(image.size)
            }
        }
    }
    
    
    
    
    func setupUI() {
        self.wantsLayer = true
        
        self.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(imageV)
        imageV.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-5)
        }
        
        self.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().offset(10)
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(selectView)
        selectView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func setData(newData:KIconGroup) {
        let title = newData.title ?? ""
        self.title.stringValue = title
        
        if let image = newData.image {
            self.imageV.image = NSImage.image(name:image)
        }
    }
    
    var imageSize:CGSize = CGSize.init(width: 26, height: 26) {
        didSet {
            self.imageV.snp.updateConstraints { (make) in
                make.size.equalTo(imageSize)
            }
        }
    }
    
    
    lazy var selectView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(40).withAlphaComponent(0.5).cgColor
        view.isHidden = true
        return view
    }()
    
    lazy var bgView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(40).withAlphaComponent(0.5).cgColor
        return view
    }()
    
    lazy var imageV: NSImageView = {
        let imageV = NSImageView.init(image: NSImage.image(name:"ic_pulldown")!)
        return imageV
    }()
    
    lazy var title: NSLabel = {
        let lab = NSLabel.label(title: "", textColor: color_gray(255), font: NSFont.systemFont(ofSize: 12))
        lab.alignment = .center
        lab.sizeToFit()
        return lab
    }()
}
